import 'package:flutter/material.dart';

class ProjectModel {
	int? id;
	int? userId;
	String? projectName;
	String? projectNo;
	int? type;
	String? declareDepartment;
	String? department;
	dynamic address;
	dynamic zip;
	String? projectManager;
	String? projectContact;
	String? phone;
	String? declareTime;
	String? background;
	String? requirements;
	String? objectives;
	String? plan;
	String? implementation;
	String? budget;
	String? budgetAmount;
	dynamic firstReviewSuggestions;
	dynamic reviewSuggestions;
	dynamic finallyReviewSuggestions;
	int? finallyReviewTime;
	dynamic reviewRecommend;
	int? status;
	dynamic firstReviewReason;
	int? bidTime;
	dynamic bindExpert;
	dynamic signupBeginTime;
	dynamic signupEndTime;
	dynamic buildDepartment;
	dynamic procurementMethod;
	dynamic bidFlag;
	dynamic badeFlag;
	dynamic manager;
	dynamic bidPrice;

	ProjectModel({
		this.id, 
		this.userId, 
		this.projectName, 
		this.projectNo, 
		this.type, 
		this.declareDepartment, 
		this.department, 
		this.address, 
		this.zip, 
		this.projectManager, 
		this.projectContact, 
		this.phone, 
		this.declareTime, 
		this.background, 
		this.requirements, 
		this.objectives, 
		this.plan, 
		this.implementation, 
		this.budget, 
		this.budgetAmount, 
		this.firstReviewSuggestions, 
		this.reviewSuggestions, 
		this.finallyReviewSuggestions, 
		this.finallyReviewTime, 
		this.reviewRecommend, 
		this.status, 
		this.firstReviewReason, 
		this.bidTime, 
		this.bindExpert, 
		this.signupBeginTime, 
		this.signupEndTime, 
		this.buildDepartment, 
		this.procurementMethod, 
		this.bidFlag, 
		this.badeFlag, 
		this.manager, 
		this.bidPrice, 
	});

	factory ProjectModel.fromJson(Map<String, dynamic> json) => ProjectModel(
				id: json['id'] as int?,
				userId: json['userId'] as int?,
				projectName: json['projectName'] as String?,
				projectNo: json['projectNo'] as String?,
				type: json['type'] as int?,
				declareDepartment: json['declareDepartment'] as String?,
				department: json['department'] as String?,
				address: json['address'] as dynamic,
				zip: json['zip'] as dynamic,
				projectManager: json['projectManager'] as String?,
				projectContact: json['projectContact'] as String?,
				phone: json['phone'] as String?,
				declareTime: json['declareTime'] as String?,
				background: json['background'] as String?,
				requirements: json['requirements'] as String?,
				objectives: json['objectives'] as String?,
				plan: json['plan'] as String?,
				implementation: json['implementation'] as String?,
				budget: json['budget'] as String?,
				budgetAmount: json['budgetAmount'] as String?,
				firstReviewSuggestions: json['firstReviewSuggestions'] as dynamic,
				reviewSuggestions: json['reviewSuggestions'] as dynamic,
				finallyReviewSuggestions: json['finallyReviewSuggestions'] as dynamic,
				finallyReviewTime: json['finallyReviewTime'] as int?,
				reviewRecommend: json['reviewRecommend'] as dynamic,
				status: json['status'] as int?,
				firstReviewReason: json['firstReviewReason'] as dynamic,
				bidTime: json['bidTime'] as int?,
				bindExpert: json['bindExpert'] as dynamic,
				signupBeginTime: json['signupBeginTime'] as dynamic,
				signupEndTime: json['signupEndTime'] as dynamic,
				buildDepartment: json['buildDepartment'] as dynamic,
				procurementMethod: json['procurementMethod'] as dynamic,
				bidFlag: json['bidFlag'] as dynamic,
				badeFlag: json['badeFlag'] as dynamic,
				manager: json['manager'] as dynamic,
				bidPrice: json['bidPrice'] as dynamic,
			);

	Map<String, dynamic> toJson() => {
				'id': id,
				'userId': userId,
				'projectName': projectName,
				'projectNo': projectNo,
				'type': type,
				'declareDepartment': declareDepartment,
				'department': department,
				'address': address,
				'zip': zip,
				'projectManager': projectManager,
				'projectContact': projectContact,
				'phone': phone,
				'declareTime': declareTime,
				'background': background,
				'requirements': requirements,
				'objectives': objectives,
				'plan': plan,
				'implementation': implementation,
				'budget': budget,
				'budgetAmount': budgetAmount,
				'firstReviewSuggestions': firstReviewSuggestions,
				'reviewSuggestions': reviewSuggestions,
				'finallyReviewSuggestions': finallyReviewSuggestions,
				'finallyReviewTime': finallyReviewTime,
				'reviewRecommend': reviewRecommend,
				'status': status,
				'firstReviewReason': firstReviewReason,
				'bidTime': bidTime,
				'bindExpert': bindExpert,
				'signupBeginTime': signupBeginTime,
				'signupEndTime': signupEndTime,
				'buildDepartment': buildDepartment,
				'procurementMethod': procurementMethod,
				'bidFlag': bidFlag,
				'badeFlag': badeFlag,
				'manager': manager,
				'bidPrice': bidPrice,
			};

	// ==================== 项目状态管理 ====================
	
	/// 项目状态常量
	static const int STATUS_NOT_SUBMITTED = 0;          // 未提交
	static const int STATUS_PENDING_PRELIMINARY = 1;     // 待初审
	static const int STATUS_PENDING_REVIEW = 2;          // 待评审
	static const int STATUS_PENDING_FINAL = 3;           // 待终审
	static const int STATUS_PRELIMINARY_REJECTED = 4;    // 初审未通过
	static const int STATUS_EXPERT_REVIEW_REJECTED = 5;  // 专家评审未通过
	static const int STATUS_FINAL_REJECTED = 6;          // 终审未通过
	static const int STATUS_PROCUREMENT_NOT_SUBMITTED = 7; // 立项/采购文件未提交
	static const int STATUS_PUBLISHED = 8;               // 已发布（采购文件已提交）
	static const int STATUS_PENDING_BIDDING = 9;         // 待评标
	static const int STATUS_BIDDING_COMPLETED = 10;      // 已评标
	static const int STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION = 11; // 初验申请
	static const int STATUS_PRELIMINARY_ACCEPTANCE_PASSED = 12;      // 初验通过
	static const int STATUS_PRELIMINARY_ACCEPTANCE_REJECTED = 13;    // 初验未通过
	static const int STATUS_FINAL_ACCEPTANCE_PASSED = 14;            // 终验通过（已归档）
	static const int STATUS_FINAL_ACCEPTANCE_REJECTED = 15;          // 终验未通过

	/// 获取项目状态文本
	String getStatusText() {
		switch (status) {
			case STATUS_NOT_SUBMITTED:
				return '未提交';
			case STATUS_PENDING_PRELIMINARY:
				return '待初审';
			case STATUS_PENDING_REVIEW:
				return '待评审';
			case STATUS_PENDING_FINAL:
				return '待终审';
			case STATUS_PRELIMINARY_REJECTED:
				return '初审未通过';
			case STATUS_EXPERT_REVIEW_REJECTED:
				return '专家评审未通过';
			case STATUS_FINAL_REJECTED:
				return '终审未通过';
			case STATUS_PROCUREMENT_NOT_SUBMITTED:
				return '立项/采购文件未提交';
			case STATUS_PUBLISHED:
				return '已发布';
			case STATUS_PENDING_BIDDING:
				return '待评标';
			case STATUS_BIDDING_COMPLETED:
				return '已评标';
			case STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION:
				return '初验申请';
			case STATUS_PRELIMINARY_ACCEPTANCE_PASSED:
				return '初验通过';
			case STATUS_PRELIMINARY_ACCEPTANCE_REJECTED:
				return '初验未通过';
			case STATUS_FINAL_ACCEPTANCE_PASSED:
				return '终验通过';
			case STATUS_FINAL_ACCEPTANCE_REJECTED:
				return '终验未通过';
			default:
				return '未知状态 $status';
		}
	}

	/// 获取项目状态颜色
	Color getStatusColor() {
		switch (status) {
			case STATUS_NOT_SUBMITTED:
				return Colors.grey;
			case STATUS_PENDING_PRELIMINARY:
			case STATUS_PENDING_REVIEW:
			case STATUS_PENDING_FINAL:
			case STATUS_PENDING_BIDDING:
			case STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION:
				return Colors.orange;
			case STATUS_PRELIMINARY_REJECTED:
			case STATUS_EXPERT_REVIEW_REJECTED:
			case STATUS_FINAL_REJECTED:
			case STATUS_PRELIMINARY_ACCEPTANCE_REJECTED:
			case STATUS_FINAL_ACCEPTANCE_REJECTED:
				return Colors.red;
			case STATUS_PROCUREMENT_NOT_SUBMITTED:
				return Colors.amber;
			case STATUS_PUBLISHED:
			case STATUS_BIDDING_COMPLETED:
			case STATUS_PRELIMINARY_ACCEPTANCE_PASSED:
				return Colors.green;
			case STATUS_FINAL_ACCEPTANCE_PASSED:
				return Colors.blueGrey;
			default:
				return Colors.grey;
		}
	}

	/// 获取项目状态描述
	String getStatusDescription() {
		switch (status) {
			case STATUS_NOT_SUBMITTED:
				return '项目申报材料尚未提交';
			case STATUS_PENDING_PRELIMINARY:
				return '项目正在等待初步审核';
			case STATUS_PENDING_REVIEW:
				return '项目正在等待专家评审';
			case STATUS_PENDING_FINAL:
				return '项目正在等待终审确认';
			case STATUS_PRELIMINARY_REJECTED:
				return '项目初审未通过，需要修改后重新提交';
			case STATUS_EXPERT_REVIEW_REJECTED:
				return '项目专家评审未通过，建议重新申报';
			case STATUS_FINAL_REJECTED:
				return '项目终审未通过，申报流程结束';
			case STATUS_PROCUREMENT_NOT_SUBMITTED:
				return '项目已立项，采购文件尚未提交';
			case STATUS_PUBLISHED:
				return '采购文件已提交，项目已发布';
			case STATUS_PENDING_BIDDING:
				return '项目正在等待评标';
			case STATUS_BIDDING_COMPLETED:
				return '项目评标已完成';
			case STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION:
				return '项目正在等待初步验收';
			case STATUS_PRELIMINARY_ACCEPTANCE_PASSED:
				return '项目初步验收已通过';
			case STATUS_PRELIMINARY_ACCEPTANCE_REJECTED:
				return '项目初步验收未通过，需要整改';
			case STATUS_FINAL_ACCEPTANCE_PASSED:
				return '项目终验通过，已完成并归档';
			case STATUS_FINAL_ACCEPTANCE_REJECTED:
				return '项目终验未通过，需要整改';
			default:
				return '项目状态信息不明确';
		}
	}

	/// 判断项目是否可以进行评审
	bool canReview() {
		return status == STATUS_PENDING_REVIEW;
	}

	/// 判断项目是否可以进行评标
	bool canBid() {
		return status == STATUS_PENDING_BIDDING;
	}

	/// 判断项目是否可以进行验收
	bool canAcceptance() {
		return status == STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION;
	}

	/// 判断项目是否处于进行中状态
	bool isInProgress() {
		return status != null && 
			   status! >= STATUS_PENDING_PRELIMINARY && 
			   status! < STATUS_FINAL_ACCEPTANCE_PASSED &&
			   ![STATUS_PRELIMINARY_REJECTED, STATUS_EXPERT_REVIEW_REJECTED, STATUS_FINAL_REJECTED, 
			      STATUS_PRELIMINARY_ACCEPTANCE_REJECTED, STATUS_FINAL_ACCEPTANCE_REJECTED].contains(status);
	}

	/// 判断项目是否已完成
	bool isCompleted() {
		return status == STATUS_FINAL_ACCEPTANCE_PASSED;
	}

	/// 判断项目是否被拒绝
	bool isRejected() {
		return [STATUS_PRELIMINARY_REJECTED, STATUS_EXPERT_REVIEW_REJECTED, STATUS_FINAL_REJECTED, 
		        STATUS_PRELIMINARY_ACCEPTANCE_REJECTED, STATUS_FINAL_ACCEPTANCE_REJECTED].contains(status);
	}

	/// 获取状态图标
	IconData getStatusIcon() {
		switch (status) {
			case STATUS_NOT_SUBMITTED:
				return Icons.edit_note;
			case STATUS_PENDING_PRELIMINARY:
			case STATUS_PENDING_REVIEW:
			case STATUS_PENDING_FINAL:
			case STATUS_PENDING_BIDDING:
			case STATUS_PRELIMINARY_ACCEPTANCE_APPLICATION:
				return Icons.pending;
			case STATUS_PRELIMINARY_REJECTED:
			case STATUS_EXPERT_REVIEW_REJECTED:
			case STATUS_FINAL_REJECTED:
			case STATUS_PRELIMINARY_ACCEPTANCE_REJECTED:
			case STATUS_FINAL_ACCEPTANCE_REJECTED:
				return Icons.cancel;
			case STATUS_PROCUREMENT_NOT_SUBMITTED:
				return Icons.upload_file;
			case STATUS_PUBLISHED:
				return Icons.publish;
			case STATUS_BIDDING_COMPLETED:
				return Icons.gavel;
			case STATUS_PRELIMINARY_ACCEPTANCE_PASSED:
				return Icons.check_circle;
			case STATUS_FINAL_ACCEPTANCE_PASSED:
				return Icons.archive;
			default:
				return Icons.help_outline;
		}
	}

	/// 静态方法：根据状态值获取状态文本
	static String getStatusTextByValue(int? statusValue) {
		final tempModel = ProjectModel(status: statusValue);
		return tempModel.getStatusText();
	}

	/// 静态方法：根据状态值获取状态颜色
	static Color getStatusColorByValue(int? statusValue) {
		final tempModel = ProjectModel(status: statusValue);
		return tempModel.getStatusColor();
	}

	/// 静态方法：根据状态值获取状态图标
	static IconData getStatusIconByValue(int? statusValue) {
		final tempModel = ProjectModel(status: statusValue);
		return tempModel.getStatusIcon();
	}
}
